Title: x86assembly - class4 - mult, div, i/o video notes
Tags: i/o

The Irvine procedure WriteString writes a null-terminated string to the console window.
The string it writes is determined by the address stored in EDX.
Extra: e.g
.data
  prompt BYTE "Input something: ", 0
.code
  mov edx, OFFSET prompt
  call WriteString
url: https://csc.csudh.edu/mmccullough/asm/help/
Topic: i/o

The Irvine procedure ReadString reads a string from the keyboard, stopping when the user presses the Enter key. The string is stored in a buffer whose address (offset) is stored in the EDX register.
ECX controls the maximum number of characters the user can enter; it should always be at least 1 less than the buffer size to save space for the null terminator.
The length of user input is stored in eax.
Extra: .data
  max = 80
  my_buffer BYTE 21 MAX+1 DUP (?)
  byteCount DWORD ?
.code
  mov edx, OFFSET my_buffer
  mov ecx, max
  call ReadString
  mov byteCount, eax
url: https://csc.csudh.edu/mmccullough/asm/help/
Topic: i/o

To convert a string to an integer, use the Irvine procedure ParseInteger32.  All valid digits from the start of the string, whose address (offset) is stored in EDX, to the first nonumeric character are converted to a 32 bit binary which is stored in the EAX register.
If the value cannot be represented as a 32 bit integer, an error is displayed on the console and the Overflow flag is set in EFLAGS.
Extra: .data
buffer BYTE "-8193"
bufsize = ($ - buffer)
intval DWORD ?
.code
    mov   edx,OFFSET buffer
    mov   ecx,bufsize
    call  ParseInteger32
    mov   intval,EAX
url: https://csc.csudh.edu/mmccullough/asm/help/
Topic: i/o

Q: To print an integer to the console, formatted with a leading sign and no leading zeros, store the integer in [...] and call the Irvine procedure [...].
A: EAX; WriteInt
Extra: mov  eax,216543
      call WriteInt
url: https://csc.csudh.edu/mmccullough/asm/help/
Topic: i/o

The x86 instruction MUL performs unsigned multiplication of the first (destination) and second (source) operand and stores the result in the implied destination operand.
The destination operand is an implied register, either AL, AX, or EAX, depending on the size of the source operand.
The result is stored in AX, DX:AX, or EDX:EAX depending on the operand size, with the high-order bits in AH, DX, or EDX respectively.
If the high-order bits of the product are 0, the CF and OF flags are cleared, otherwise they are set.
Extra: mov ax, 5
mov cx, 10
mul cx		; ax = 50 (technically dx:ax=50)
url: https://www.felixcloutier.com/x86/mul
Topic: *

With MUL, for an example, if AX is multiplied by a 16 bit operand, the product is stored in the combined DX and AX registers. The high 16 bits are stored in DX and the low 16 bits are stored in AX.
The Carry flag will be set if DX is not equal to 0, indicating the product does not fit into the lower half of the implied destination operand.
url: https://www.felixcloutier.com/x86/mul
Topic: i/o

The x86 instruction IMUL performs a signed multiplication of two operands. It preserves sign by sign-extending the sign bit to most-significant-bit of the destination operand.
As with MUL, the length of the product is twice the length of the operands.
IMUL can take 1-3 operands. The one-operand form is similar to MUL and uses the A registers as the destination operand.
The two-operand form allows explicitly inputting a destination operand (a general purpose register) as the first operand.
The three-operand form has a destination operand as the first operand, but it isn't used for calculation; the second and third operands (both of which do not have to be registers) are multiplied together and stored in the first operand.
Extra: mov al, 48
mov bl, 4
imul bl      ; AX = +192, OF = 1
mov ax, 48
mov bx, 4
imul bx	     ; DX:AX = 0:+192 , OF = 0
url: https://www.felixcloutier.com/x86/imul
Topic: multiply

The DIV instruction divides unsigned the value in AX, DX:AX, EDX:EAX, or RDX:RAX by the source operand (divisor), stores the whole-number part of the result in the A (AL, AX, EAX, RAX)  register, and stores the remainder in the D register (except for byte division, then it stores remainder in AH). Which register(s) depends on the size of the source operand;
Non integral results are truncated.
url: https://www.felixcloutier.com/x86/div
Topic: divide

The IDIV instruction divides (signed) the value in the AX, DX:AX, or EDX:EAX (dividend) by the source operand (divisor) and stores the result in the AX, DX:AX, or EDX:EAX register.
As with the DIV instruction, the whole part of the result is stored in the A register and the remainder is stored in the D register, except for 8 bit division, where the remainder is stored in the AH register.
Before dividing, the dividend in the A register must be sign-extended (usually into the D register).  That is accomplished with the 0 operand instruction cwd
Extra: .data
  wordVal SWORD -5000
.code
  mov ax, wordVal
  cwd                   ; sign extend
  mov bx, +256
  idiv bx		; quotient AX = -19, rem DX = -136
url: https://www.felixcloutier.com/x86/idiv
Topic: divide